﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SuperQuiz.Logic;
using SuperQuiz.Model;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace SuperQuiz
{
    public sealed partial class QuizPage : SuperQuiz.Common.LayoutAwarePage
    {
        public QuizPage()
        {
            this.InitializeComponent();
            QuizApplication.Instance.SetupSettingsEvents(this);
        }

        protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            if (pageState != null && pageState.ContainsKey("QuizId"))
            {
                navigationParameter = pageState["QuizId"];
            }
            await QuizApplication.Instance.AwaitLoad();
            var quiz = QuizApplication.Instance.GetQuiz((String)navigationParameter);
            this.DefaultViewModel["QuizApplication"] = QuizApplication.Instance;
            this.DefaultViewModel["Category"] = quiz.Category;
            this.flipView.SelectedItem = quiz;
        }

        protected override void SaveState(Dictionary<String, Object> pageState)
        {
            var quiz = (Quiz)this.flipView.SelectedItem;
            pageState["QuizId"] = quiz.UniqueId;
        }

        private void PlayClick(object sender, RoutedEventArgs e)
        {
            var quiz = (Quiz)this.flipView.SelectedItem;
            var quizPlay = QuizApplication.Instance.StartQuiz(quiz);
            this.Frame.Navigate(typeof(QuizPlayPage), quizPlay.Quiz.UniqueId);
        }
    }
}
